package com.bosicen.crm.web;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.Writer;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/****
 * 验证码
 * @author niyong
 *
 */
@Controller
@RequestMapping("/imagecode")
public class ImageCodeController {
	private static final Random random = new Random();

	 private static final Font[] CODEFONT = { new Font("宋体", Font.BOLD, 25), new Font("宋体", Font.BOLD, 25), new Font("宋体", Font.BOLD, 25),
	   new Font("宋体", Font.BOLD, 25) };
	 private static final Color[] FONTCOLOR = { Color.ORANGE, Color.RED, Color.PINK, Color.BLUE, Color.GREEN };
	 private static final Color[] BGCOLOR = { Color.WHITE, Color.WHITE };
	 private static final Color LINECOLOR = new Color(242, 234, 22);
//	 private static final String[] CODE = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
//	   "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
	 private static final String[] CODE = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
	 private static StringBuffer CONDENUMBER = null;
	 private static final int WIDTH = 100, HEIGHT = 25;

	 @RequestMapping(value="/findimagecode")
	 public  void image(HttpServletRequest request, HttpServletResponse response) {
	  CONDENUMBER = new StringBuffer();
	  BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
	  Graphics g = image.getGraphics();
	  g.setColor(BGCOLOR[random.nextInt(2)]);
	  g.fillRect(0, 0, WIDTH, HEIGHT);
	  for (int i = 0; i < 4; i++) {
	   drawCode(g, i);
	  }
	  drawNoise(g, 8);
	  g.setColor(Color.gray);
	  g.drawRect(0, 0, WIDTH - 1, HEIGHT - 1);
	  g.dispose();
	  response.setHeader("Pragma", "No-cache");
	  response.setHeader("Cache-Control", "no-cache");
	  response.setDateHeader("Expires", 0);
	  response.setContentType("image/png");
	  ServletOutputStream sos = null;
	  try {
	   sos = response.getOutputStream();
	   ImageIO.write(image, "png", sos);
	   sos.flush();
	   sos.close();
	   request.getSession().setAttribute("imagecode_id", CONDENUMBER.toString());
	  } catch (Exception e) {
	   e.printStackTrace();
	  }

	 }

	 private final void drawCode(Graphics graphics, int i) {
	  String number = CODE[random.nextInt(10)];
	  graphics.setFont(CODEFONT[random.nextInt(4)]);
	  graphics.setColor(FONTCOLOR[random.nextInt(5)]);
	  graphics.drawString(number, 10 + i * 20, 20);
	  CONDENUMBER.append(number);
	 }

	 private final void drawNoise(Graphics graphics, int lineNumber) {
	  graphics.setColor(LINECOLOR);
	  int pointX1, pointY1, pointX2, pointY2;
	  for (int i = 0; i < lineNumber; i++) {
	   pointX1 = 1 + (int) (Math.random() * WIDTH);
	   pointY1 = 1 + (int) (Math.random() * HEIGHT);
	   pointX2 = 1 + (int) (Math.random() * WIDTH);
	   pointY2 = 1 + (int) (Math.random() * HEIGHT);
	   graphics.drawLine(pointX1, pointY1, pointX2, pointY2);
	  }
	 }
}
